<?php
/**
 * $Id: cms_assets_controller.php 196 2011-11-09 06:25:46Z manu $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 196 $
 * @lastModifiedBy		$LastChangedBy: manu $
 * @lastChanged			$Date: 2011-11-09 11:55:46 +0530 (Wed, 09 Nov 2011) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Base class for handling assets. 
 * Needs permission before editing. If you need to overwrite a function, please do so in assets_controller.php
 *
 */
class CmsAssetsController extends AppController {

	var $name = 'Assets';
	var $helpers = array('Html', 'Form','Assets.Asset','Time', 'Session','Cms.Restricted','Assets.Gallery', 'Backend.Backend');
	var $components =array('Auth','Session');

	function beforeFilter()
	{
		$this->Auth->allow(array('view','viewWithComments'));
		//$this->Auth->authorize = array('model'=>'User');
		$this->Auth->loginAction = '/users/login';
		parent::beforeFilter();
	}
	
	function getModel()
	{
		$modelName = $this->modelNames['0'];
		return $this->$modelName;
	}
	
	function getModelAlias()
	{
		$modelName = $this->modelNames['0'];
		return $modelName;		
	}

	/**
	 * Add function for assets, handle file uploads and description of an asset of $type for $itemId
	 *
	 * @param unknown_type $type
	 * @param unknown_type $itemId
	 */
	function add($type = null, $itemId = null)
	{
		$type = ucfirst($type);
		App::import('Model',$type);
		$typeModel = new $type();
		$item = $this->getModel()->getItem($type,$itemId);
		$this->checkPluginAccess($type);
		if(empty($item))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect($this->referer());
		}

		if (!empty($this->data))
		{
			$this->getModel()->create();
			$this->data[$this->getModelAlias()]['user_id'] = $this->Session->read('Auth.User.id');
			if($this->getModel()->saveAll($this->data))
			{
				$this->__afterAssetAdd($this->data,$item);				
			}
			else
			{
				$this->__afterAssetFail($this->data,$item);				
			}
		}
		elseif (empty($this->data))
		{
			$redirectUrl = '/';
			$this->Session->write('redirectTo',$redirectUrl);
		}
		$this->pageTitle = sprintf(__('Upload an image  for %s %s'),$type,$item[$type][$typeModel->displayField]);
		$this->set('type',$type);
		$this->set('item_id',$itemId);
		$this->set('modelName',$this->getModelAlias());
		$this->set('pageHeading',sprintf(__('Upload an image  for %s %s'),$type,$item[$type][$typeModel->displayField]));

	}
	
	function __afterAssetAdd($uploadedData,$itemInfo)
	{
		$this->successMsg(__('The file has been saved', true));
		$this->redirect($this->Session->read('redirectTo'));			
	}
	
	function __afterAssetFail($uploadedData,$itemInfo)
	{
		$this->errorMsg(__('The File could not be saved. Please try again.', true));
	}

	/**
	 * Deprecated function. Not sure if this will be reusable for next versions.
	 *
	 * @param unknown_type $type
	 * @param unknown_type $itemId
	 */
	function multi_upload($type, $itemId)
	{
		$this->checkPluginAccess($type);
		
		if(!$this->Session->read('last_added_offer'))
		{
			$this->errorMsg(__('You don\'t have permission to access',true));
			$this->redirect('/users/myAccount');
			exit;
		}
		$max_files = 5;
		$this->set('max_files', $max_files);

		if (!empty($this->data)) {
			//prd($this->data);
			if($this->getModel()->saveAll($this->data[$this->getModelAlias()]))
			{
				$this->successMsg(__('The file(s) have been saved', true));
				$this->redirect('/offers/preview');
			} else {
				$this->errorMsg(__('The files could not be saved. Please try again.', true));
			}
		}

		if (empty($this->data)) {
			// set uploaded files
			$this->_setUploadedFiles($this->Session->read('last_added_offer'));
		}
	}

	/**
	 * Invalid function. Not removing because it might be useful in future
	 *
	 * @param unknown_type $offer_id
	 */
	function _setUploadedFiles($offer_id)
	{
		//$assets = $this->Asset->Offer->findbyId($offer_id);
		//pr($assets);
		//$this->set('assets', $assets['Asset']);
	}

	/**
	 * TODO: Delete media, after checking permissions
	 *
	 * @param unknown_type $id
	 */
	function delete($id=null)
	{
		if($id)
		{
			if($this->Asset->del($id))
			{
				$this->successMsg(__('Media has been deleted'));
			}
		}
		$this->redirect($this->referer());
	}
	
	/**
	 * Simple View page for asset
	 *
	 * @param unknown_type $id
	 */
	function view($id)
	{
		if (!$id) {
			$this->flash(__('Invalid ID', true), $this->referer());
		}
		$media = $this->Asset->read(null, $id);
		$this->set('media', $media);		
	}
	
	/**
	 * Asset view page with attractive comments.
	 * This will always be used through routes.
	 *
	 * @param unknown_type $id
	 */
	function viewWithComments($id)
	{
		if (!$id) {
			$this->flash(__('Invalid ID', true), $this->referer());
		}
		$this->getModel()->recursive = 0;
		$media = $this->getModel()->read(null, $id);
		$this->set('media', $media);
		$this->pageTitle = ucwords($media[$media[$this->getModel()]['type']]['name']).'\'s Gallery  ';
		$this->helpers[] = 'Comments.Comment';
		$this->_setRelatedMediaIds($id, $media[$this->getModel()]['type'], $media[$this->getModel()]['item_id']);
	}
	
	/**
	 * Somebody, please document this.
	 *
	 * @param unknown_type $id
	 * @param unknown_type $type
	 * @param unknown_type $itemId
	 */
	function _setRelatedMediaIds($id, $type, $itemId)
	{
		$this->Asset->unbindAll(false);
		if($nextId = $this->Asset->find(array('Asset.type'=>$type, 'Asset.item_id'=>$itemId, 'Asset.id >'=>$id), array('id')))
		{
			$this->set('nextId',$nextId['Asset']['id']);
		}
		if($previousId = $this->Asset->find(array('Asset.type'=>$type, 'Asset.item_id'=>$itemId, 'Asset.id <'=>$id), array('id'), 'Asset.id desc'))
		{
			$this->set('previousId',$previousId['Asset']['id']);
		}
	}
	
	function admin_delete($id)
	{
		if(!$id)
		{
			$this->errorMsg("Invalid URL.");
			$this->redirect($this->referer());
		}
		if($this->getModel()->del($id))
		{
			$this->successMsg(__("Image have been deleted."));
			
		}
		$this->redirect($this->referer());
	}

}
?>